package com.conncui.subscript.adapter;

import java.util.List;
import java.util.Map;

import com.bumptech.glide.Glide;
import com.conncui.common.tool.Utility;
import com.conncui.subscript.R;
import com.conncui.subscript.SubscriptDrawable;
import com.conncui.subscript.SubscriptHelper;
import com.conncui.subscript.location.AnchorLocation;
import com.conncui.subscript.location.RelativePosition;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.widget.BaseAdapter;

public class OverLayAdapterV2 extends BaseAdapter {
    static String TAG = OverLayAdapterV2 .class.getSimpleName();
    Context mContext;
    List<Map<String, Object>> mDataList;
    private LayoutInflater mLayoutInflater;

    class ItemViewPreDrawListener implements OnPreDrawListener {
        final ItemViewHolderV2 viewHolder;

        ItemViewPreDrawListener(ItemViewHolderV2 itemViewHolderV2) {
            viewHolder = itemViewHolderV2;
        }

        public boolean onPreDraw() {
            
            SubscriptHelper.add(
                    viewHolder.getRelativeLayout1(),
                    viewHolder.getImageButton1(),
                    new AnchorLocation(1.0f, 0.0f),
                    viewHolder.getViewOutsideRightTopSubscript(),
                    new AnchorLocation(1.0f, 0.0f),
                    new RelativePosition(Utility.dip2px(OverLayAdapterV2.this.mContext, 3.0f), Utility.dip2px(OverLayAdapterV2.this.mContext, 3.0f)));
            SubscriptHelper.add(viewHolder.getItemView(), viewHolder.getButton1(), new AnchorLocation(0.0f, 0.5f), viewHolder.getViewOutsideLeftSubscript6Dp(), new AnchorLocation(1.0f, 0.5f), new RelativePosition(-Utility.dip2px(OverLayAdapterV2.this.mContext, 6.0f), 0.0f));
            SubscriptHelper.add(viewHolder.getItemView(), viewHolder.getButton1(), new AnchorLocation(0.0f, 0.5f), viewHolder.getViewOutsideLeftSubscript6Dp(), new AnchorLocation(1.0f, 0.5f), new RelativePosition(-Utility.dip2px(OverLayAdapterV2.this.mContext, 16.0f), 0.0f));
            return true;
        }
    }

    public OverLayAdapterV2(Context context, List<Map<String, Object>> list) {
        this.mContext = context;
        this.mLayoutInflater = LayoutInflater.from(context);
        this.mDataList = list;
    }

    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    public int getViewTypeCount() {
        return super.getViewTypeCount();
    }

    public int getCount() {
        return this.mDataList.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        SubscriptDrawable mViewOutSideRightTopSubscripts =null;
        SubscriptDrawable mViewOutSideLeftSubscripts6Dp =null;
        SubscriptDrawable mViewOutSideLeftSubscripts16Dp =null;
        ItemViewHolderV2 viewHolder;
        Log.i(TAG,"convertView:"+(convertView==null?"null":Integer.toString(convertView.hashCode())));
        if (convertView == null) {
            convertView = this.mLayoutInflater.inflate(R.layout.item_listview_over_lay_2, parent, false);
            viewHolder = new ItemViewHolderV2(convertView);


            mViewOutSideRightTopSubscripts = new SubscriptDrawable(this.mContext, new AnchorLocation(1.0f, 0.0f));
            mViewOutSideLeftSubscripts6Dp = new SubscriptDrawable(this.mContext, new AnchorLocation(1.0f, 0.5f));
            mViewOutSideLeftSubscripts16Dp = new SubscriptDrawable(this.mContext, new AnchorLocation(1.0f, 0.5f));
            viewHolder.setItemView(convertView);
            viewHolder.setViewOutsideRightTopSubscript(mViewOutSideRightTopSubscripts);
            viewHolder.setViewOutsideLeftSubscript6Dp(mViewOutSideLeftSubscripts6Dp);
            viewHolder.setViewOutsideLeftSubscript16Dp(mViewOutSideLeftSubscripts16Dp);
            viewHolder.getRelativeLayout1().getViewTreeObserver().addOnPreDrawListener(new ItemViewPreDrawListener(viewHolder));
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ItemViewHolderV2) convertView.getTag();
            mViewOutSideRightTopSubscripts = viewHolder.getViewOutsideRightTopSubscript();
            mViewOutSideLeftSubscripts6Dp = viewHolder.getViewOutsideLeftSubscript6Dp();
            mViewOutSideLeftSubscripts16Dp = viewHolder.getViewOutsideLeftSubscript16Dp();
        }
        Glide.with(mContext).load(((Integer) ((Map) this.mDataList.get(position)).get(ConstantFlag.KEY_ImageButton)).intValue()).into(viewHolder.getImageButton1());
        Glide.with(mContext).load(((Integer) ((Map) this.mDataList.get(position)).get(ConstantFlag.KEY_ZoomButton)).intValue()).into(viewHolder.getZoomButton1());
//        viewHolder.getButton1().setBackgroundResource(((Integer) ((Map) this.mDataList.get(position)).get(ConstantFlag.KEY_Button)).intValue());

//        viewHolder.getButton1().setText(""+viewHolder.getButton1().hashCode());
//        viewHolder.getTextView1().setText("position:" + position);
//        viewHolder.getCheckBox1().setChecked(((Boolean) ((Map) this.mDataList.get(position)).get(ConstantFlag.KEY_CheckBox)).booleanValue());
//        viewHolder.getTextView2().setText(""+viewHolder.hashCode());

        mViewOutSideRightTopSubscripts.setText(position > 150 ? "A" : ""+Integer.valueOf(position));
        mViewOutSideLeftSubscripts16Dp.setText(position > 150 ? "A" : ""+Integer.valueOf(position));
        mViewOutSideLeftSubscripts6Dp.setText(position > 150 ? (position>160?"":"A") : ""+Integer.valueOf(position));

        return convertView;
    }

    public List<Map<String, Object>> getList() {
        return this.mDataList;
    }

    public void setList(List<Map<String, Object>> list) {
        this.mDataList = list;
    }
}
